;
(function($, windw, document, undefined) {
	var selectTable = function myTable(node,selection, data, setting) {
		this.node = node;
		this.data = data;
		this.selection = selection;
		var defaultOpt = {
			callback : {
				onclick : function() {

				}
			}
		}
		this.opt = setting;
	};
	selectTable.prototype = {
		init : function() {
			var str = "";
			var allChecked = this.data.length == this.selection.length ? "icon-check" : "icon-check-empty"
            str += "<div class='kr-checked list02' style='display: block; width: 100%;'>";
            str += " <ul>" +
                        "<li><span class='select-all "+ allChecked +"' data-id='all'>全选</li>";
            for (var i = 0; i < this.data.length; i++) {
                var currChecked = this.selection.indexOf(this.data[i].id) >= 0 ? "icon-check" : "icon-check-empty"
                str += "<li><span class='"+ currChecked +"' data-id='"+ this.data[i].id +"' data-name='"+ this.data[i].name +"'>"+this.data[i].name+"</span></li>";
            }
            str += "</ul></div>";
			this.node.empty();
			this.node.html(str);
			var self = this
			$(".kr-checked span").each(function(){
                $(this).on("click", function() {
                    if ($(this).hasClass("icon-check")) {
                        $(this).addClass("icon-check-empty").removeClass("icon-check");
                    } else {
                        $(this).removeClass("icon-check-empty").addClass("icon-check");
                    }
                    var id = $(this).data("id")
                    var name = $(this).data("name")
                    var flag = $(this).hasClass("icon-check")
                    // 如果点击的是全选
                    if (id == "all" && flag) {
                        $(".kr-checked span").each(function(){
                            $(this).removeClass("icon-check-empty").addClass("icon-check");
                        })
                    } else if (id == "all" && !flag) {
                        $(".kr-checked span").each(function(){
                            $(this).addClass("icon-check-empty").removeClass("icon-check");
                        })
                    } else if (flag) {
                        $(".kr-checked span[data-id='all']").removeClass("icon-check-empty").addClass("icon-check")
                        $(".kr-checked span").each(function(){
                            if(!$(this).hasClass("icon-check")){
                                $(".kr-checked span[data-id='all']").removeClass("icon-check").addClass("icon-check-empty")
                                return false
                            }
                        })
                    } else {
                        $(".kr-checked span[data-id='all']").removeClass("icon-check").addClass("icon-check-empty")
                    }
                    var result = []
                    $(".kr-checked span").each(function(){
                        if ($(this).hasClass("icon-check") && $(this).data("id") != "all") {
                            result.push({
                                id: $(this).data("id"),
                                name: $(this).data("name")
                            })
                        }
                    })
                    self.opt.callback.onClick(result);
                });
            });
		},
	};
	$.fn.selectTable = function(selection, data, setting) {
		var t = new selectTable(this, selection, data, setting);
		t.init();
	}
})(jQuery, window, document);
